﻿using RndMaterialDisposalService.Models;
using RndMaterialDisposalService.Models.Entitys;
using RndMaterialDisposalService.Models.RequestModel;
using AutoMapper;
using Masuit.Tools;
using Microsoft.AspNetCore.Mvc;
using System.Data;

namespace RndMaterialDisposalService.AutoMapperProfile
{
    /// <summary>
    /// 
    /// </summary>
    public class CapitalScrapReportProfile : Profile
    {



        /// <summary>
        /// 
        /// </summary>
        public CapitalScrapReportProfile()
        {
            CreateMap<ScrapApplicationModel, CapitalScrapReportDto>()
              .ForMember(dest => dest.SysId, opt => opt.MapFrom(src => Guid.NewGuid()))
              .ForMember(dest => dest.ApplicantId, opt => opt.MapFrom(src => src.Applicant))
              .ForMember(dest => dest.LogisticsApprover, opt => opt.MapFrom(src => src.Approver.LogisticsDepartment != null ? src.Approver.LogisticsDepartment.userName : ""))
              ;


            CreateMap<ERPAssetInfoModel, CapitalScrapReportSubtableDto>()
              .ForMember(dest => dest.SysId, opt => opt.MapFrom(src => Guid.NewGuid()))
              .ForMember(dest => dest.TotalDeprecition, opt => opt.MapFrom(src => string.IsNullOrEmpty(src.TotalDeprecition) ? "0" : src.TotalDeprecition))
              .ForMember(dest => dest.NetWorth, opt => opt.MapFrom(src => src.NetValue));


            CreateMap<VehicleMaintenanceReq, VehicleDispatchReportDto>()
              .ForMember(dest => dest.SysId, opt => opt.MapFrom(src => Guid.NewGuid()))
              .ForMember(dest => dest.Folio, opt => opt.MapFrom(src => src.FormHeader.ProcFolio!.Trim()))
              .ForMember(dest => dest.Subject, opt => opt.MapFrom(src => src.FormHeader.Subject!.Trim()))
              .ForMember(dest => dest.ProcInstId, opt => opt.MapFrom(src => src.FormHeader.ProcInstId))
              .ForMember(dest => dest.ActivityName, opt => opt.MapFrom(src => src.FormHeader.NextActivityName))
              .ForMember(dest => dest.ApplicationDate, opt => opt.MapFrom(src => src.ApplicantDate))
              .ForMember(dest => dest.Applicant, opt => opt.MapFrom(src => src.Signature))
              .ForMember(dest => dest.BudgetCosts, opt => opt.MapFrom(src => src.UpkeepCosts))
              .ForMember(dest => dest.CarMaintain, opt => opt.MapFrom(src => string.Join(";", src.CarMaintain)))
              .ForMember(dest => dest.CarEngineSanqing, opt => opt.MapFrom(src => string.Join(";", src.CarEngineSanqing)))
              ;

            CreateMap<InternalVehicleFormDataReq, InternalVehicleReportDto>()
             .ForMember(dest => dest.SysId, opt => opt.MapFrom(src => Guid.NewGuid()))
             .ForMember(dest => dest.Folio, opt => opt.MapFrom(src => src.FormHeader.ProcFolio!.Trim()))
             .ForMember(dest => dest.Subject, opt => opt.MapFrom(src => src.FormHeader.Subject!.Trim()))
             .ForMember(dest => dest.ProcInstId, opt => opt.MapFrom(src => src.FormHeader.ProcInstId))
             .ForMember(dest => dest.ActivityName, opt => opt.MapFrom(src => src.FormHeader.NextActivityName))
             .ForMember(dest => dest.UseCarTime, opt => opt.MapFrom((src, dest) => src.UseCarTime?.TimeOfDay ?? null))
             .ForMember(dest => dest.ReturnTime, opt => opt.MapFrom((src, dest) => src.ReturnTime?.TimeOfDay ?? null))
             ;

            CreateMap<InternalVehicleFormDataReq, ConfirmationExpenseDto>()
             .ForMember(dest => dest.SysId, opt => opt.MapFrom(src => Guid.NewGuid()))
             .ForMember(dest => dest.ProcFolio, opt => opt.MapFrom(src => src.FormHeader.ProcFolio!.Trim()))
             .ForMember(dest => dest.Subject, opt => opt.MapFrom(src => src.FormHeader.Subject!.Trim()))
             .ForMember(dest => dest.ProcInstId, opt => opt.MapFrom(src => src.FormHeader.ProcInstId))
             .ForMember(dest => dest.FormId, opt => opt.MapFrom(src => src.FormHeader.FormId))
             .ForMember(dest => dest.CarType, opt => opt.MapFrom(src => src.VehicleRequirement))
             .ForMember(dest => dest.UseDeptNameCode, opt => opt.MapFrom(src => $"{src.DeptCode} {src.DeptName}"))
             .ForMember(dest => dest.UseCarMobile, opt => opt.MapFrom(src => src.UserMobile))
             .ForMember(dest => dest.UseCarDate, opt => opt.MapFrom((src, dest) => {
                 if (src.UseCarDate == null || src.UseCarTime == null) return DateTime.Now;
                 var dateStr = src.UseCarDate!.Value.ToLocalTime().ToString("yyyy-MM-dd");;
                 var timeStr = src.UseCarTime!.Value.ToLocalTime().ToString("HH:mm:ss");
                 return DateTime.Parse($"{dateStr} {timeStr}");
             }))
             .ForMember(dest => dest.ReturnDate, opt => opt.MapFrom((src, dest) => {
                 if (src.ReturnDate == null || src.ReturnTime == null) return new Nullable<DateTime>();
                 var dateStr = src.ReturnDate!.Value.ToLocalTime().ToString("yyyy-MM-dd"); ;
                 var timeStr = src.ReturnTime!.Value.ToLocalTime().ToString("HH:mm:ss");
                 return DateTime.Parse($"{dateStr} {timeStr}");
             }))
             .ForMember(dest => dest.VehicleRequirement, opt => opt.MapFrom(src => src.DetailDescription))
             ;


            CreateMap<InternalVehicleReportDto, InternalVehicleReportExportModel>()
             .ForMember(dest => dest.ApplicantDate, opt => opt.MapFrom(src => src.ApplicantDate.ToString("yyyy-MM-dd HH:mm:ss")))
             .ForMember(dest => dest.UseCarDate, opt => opt.MapFrom(src => src.UseCarDate.HasValue == true ? src.UseCarDate.Value.ToString("yyyy-MM-dd") : ""))
             .ForMember(dest => dest.UseCarTime, opt => opt.MapFrom(src => src.UseCarTime.HasValue == true ? src.UseCarTime.Value.ToString(@"HH\:mm") : ""))
             .ForMember(dest => dest.ReturnDate, opt => opt.MapFrom(src => src.ReturnDate.HasValue == true ? src.ReturnDate.Value.ToString("yyyy-MM-dd") : ""))
             .ForMember(dest => dest.ReturnTime, opt => opt.MapFrom(src => src.ReturnTime.HasValue == true ? src.ReturnTime.Value.ToString(@"HH\:mm") : ""))
             .ForMember(dest => dest.LongDistance, opt => opt.MapFrom(src => src.LongDistance == 1 ? "是" : "否"))
             .ForMember(dest => dest.NoNeedBack, opt => opt.MapFrom(src => src.NoNeedBack == 1 ? "是" : "否"))
             ;



            CreateMap<NewProductApplyFormData, NewProductApplyReportDto>()
             .ForMember(dest => dest.SysId, opt => opt.MapFrom(src => Guid.NewGuid()))
             .ForMember(dest => dest.Folio, opt => opt.MapFrom(src => src.FormHeader.ProcFolio!.Trim()))
             .ForMember(dest => dest.Subject, opt => opt.MapFrom(src => src.FormHeader.Subject!.Trim()))
             .ForMember(dest => dest.ProcInstId, opt => opt.MapFrom(src => src.FormHeader.ProcInstId))
             .ForMember(dest => dest.ActivityName, opt => opt.MapFrom(src => src.FormHeader.NextActivityName))
             .ForMember(dest => dest.ProductModelReamrk, opt => opt.MapFrom((src,dest) => src.ProductModelReamrk.Join(",")))
             ;


            CreateMap<NewProductApplyReportDto, LaunchVerifyFormData>()
             .ForMember(dest => dest.OrginProcInstId, opt => opt.MapFrom(src => src.ProcInstId))
             .ForMember(dest => dest.OrginFolio, opt => opt.MapFrom(src => src.Folio))
             ;


            CreateMap<NewProductApplyReportDto, NewProductApplyReportExportModel>()
             .ForMember(dest => dest.ApplicantDate, opt => opt.MapFrom(src => src.ApplicantDate.ToString("yyyy-MM-dd")))
             ;


            CreateMap<LaunchVerifyFormData, LaunchVerifyReportDto>()
             .ForMember(dest => dest.SysId, opt => opt.MapFrom(src => Guid.NewGuid()))
             .ForMember(dest => dest.Folio, opt => opt.MapFrom(src => src.FormHeader.ProcFolio!.Trim()))
             .ForMember(dest => dest.Subject, opt => opt.MapFrom(src => src.FormHeader.Subject!.Trim()))
             .ForMember(dest => dest.ProcInstId, opt => opt.MapFrom(src => src.FormHeader.ProcInstId))
             .ForMember(dest => dest.ActivityName, opt => opt.MapFrom(src => src.FormHeader.NextActivityName))
             .ForMember(dest => dest.CreaterTime, opt => opt.MapFrom(src => DateTime.Now))
             .ForMember(dest => dest.RelationFolio, opt => opt.MapFrom(src => src.OrginFolio))
             .ForMember(dest => dest.RelationProcInstId, opt => opt.MapFrom(src => src.OrginProcInstId))
             .ForMember(dest => dest.ProductModelNumber, opt => opt.MapFrom((src,dest) => string.Join(";", src.ProductModelNumber)))
             ;

            CreateMap<LaunchVerifyReportDto, LaunchVerifyReportExportModel>()
             .ForMember(dest => dest.ApplicantDate, opt => opt.MapFrom(src => src.ApplicantDate.ToString("yyyy-MM-dd")))
             .ForMember(dest => dest.ProjectStartDate, opt => opt.MapFrom(src => src.ProjectStartDate == null ? "" : src.ProjectStartDate.Value.ToString("yyyy-MM-dd")))
             .ForMember(dest => dest.ProjectEndDate, opt => opt.MapFrom(src => src.ProjectEndDate == null ? "" : src.ProjectEndDate.Value.ToString("yyyy-MM-dd")))
             ;


            CreateMap<TrackMaterialTrackingDetails, MaterialTrackingDetails>()
                //.BeforeMap((src, dest) => {
                //    src.engineerID = dest.engineerID;
                //    src.engineerName = dest.engineerName;
                //});
                ;

            CreateMap<(TheMainFlowFormDataModel formDataModel,string mainFlowId, string dataKey), WorkshopProcessinFormData>()
             .ForMember(dest => dest.MaterialCode, opt => opt.MapFrom((src, dest) => src.formDataModel.trialApplication?.RawMaterialCode))
             .ForMember(dest => dest.ProductType, opt => opt.MapFrom((src, dest) => src.formDataModel.trialApplication?.RawProductType))
             .ForMember(dest => dest.Manufacturer, opt => opt.MapFrom((src, dest) => src.formDataModel.trialApplication?.RawManufacturer))
             .ForMember(dest => dest.NewMaterialCode, opt => opt.MapFrom((src, dest) => src.formDataModel.trialApplication?.NewMaterialCode))
             .ForMember(dest => dest.NewProductType, opt => opt.MapFrom((src, dest) => src.formDataModel.trialApplication?.NewProductType))
             .ForMember(dest => dest.NewManufacturer, opt => opt.MapFrom((src, dest) => src.formDataModel.trialApplication?.NewManufacturer))
             .ForMember(dest => dest.ParentFlowId, opt => opt.MapFrom(src => src.mainFlowId))
             .ForMember(dest => dest.CabinetCode, opt => opt.MapFrom((src, dest) => {
                 var item = src.formDataModel.materialTrackingDetailsList.FirstOrDefault(p => p.key!.ToString() == src.dataKey) ?? new MaterialTrackingDetails();
                 dest.CabinetName = item.usingSingleBoardCabinetCount;
                 dest.ProductNumber = item.singleBoardCabinetType;
                 dest.ElementLocation = item.tagNumber;
                 return item.trialBoardCabinetCode;
             }))
             .ForMember(dest => dest.EngineerID, opt => opt.MapFrom((src, dest) =>
             {
                 var item = src.formDataModel.materialTrackingDetailsList.FirstOrDefault(p => p.key!.ToString() == src.dataKey) ?? new MaterialTrackingDetails();
                 var userNamelist = new List<string?>();
                 userNamelist.Add(item.engineerName);
                 userNamelist.Add(src.formDataModel.trialApplication?.ComponentEngineer);
                 userNamelist.Add(src.formDataModel.trialApplication?.SQEEngineer);
                 dest.EngineerName = userNamelist.Distinct().Join(";");

                 var userlist = new List<string?>();
                 userNamelist.Add(item.engineerID);
                 userlist.Add(src.formDataModel.trialApplication?.ComponentEngineerId);
                 userlist.Add(src.formDataModel.trialApplication?.SQEEngineerId);

                 dest.Task8ApproverList.Add(new ConsignerModel()
                 {
                     SysId = Guid.NewGuid(),
                     Approver = src.formDataModel.trialApplication?.ComponentEngineer,
                     ApproverId = src.formDataModel.trialApplication?.ComponentEngineerId,
                     RoleType = "ComponentEngineer",
                     RoleTypeName = "成本经理"
                 });
                 dest.Task8ApproverList.Add(new ConsignerModel()
                 {
                     SysId = Guid.NewGuid(),
                     Approver = item.engineerName,
                     ApproverId = item.engineerID,
                     RoleType = "EngineerName",
                     RoleTypeName = "产品工程师"
                 });
                 dest.Task8ApproverList.Add(new ConsignerModel()
                 {
                     SysId = Guid.NewGuid(),
                     Approver = src.formDataModel.trialApplication?.SQEEngineer,
                     ApproverId = src.formDataModel.trialApplication?.SQEEngineerId,
                     RoleType= "SQEEngineer",
                     RoleTypeName = "SQE工程师"
                 });

                 return userlist.Distinct().Join(";");
             }))
             .ForMember(dest => dest.Oem, opt => opt.MapFrom((src, dest) => src.formDataModel.trialApplication?.Oem))
             .ForMember(dest => dest.MaterialPreparationParty, opt => opt.MapFrom((src, dest) => src.formDataModel.trialApplication?.MaterialPreparationParty))
             //.IncludeBase<TrackMaterialTrackingDetails, MaterialTrackingDetails>()
             ;



            CreateMap<WorkshopProcessinFormData, WorkshopProcessinReportDto>()
             .ForMember(dest => dest.SysId, opt => opt.MapFrom(src => Guid.NewGuid()))
             .ForMember(dest => dest.Folio, opt => opt.MapFrom(src => src.FormHeader.ProcFolio!.Trim()))
             .ForMember(dest => dest.Subject, opt => opt.MapFrom(src => src.FormHeader.Subject!.Trim()))
             .ForMember(dest => dest.ProcInstId, opt => opt.MapFrom(src => src.FormHeader.ProcInstId))
             .ForMember(dest => dest.ActivityName, opt => opt.MapFrom(src => src.FormHeader.NextActivityName))
             //.ForMember(dest => dest.CreaterTime, opt => opt.MapFrom(src => DateTime.Now))
             //.ForMember(dest => dest.RelationFolio, opt => opt.MapFrom(src => src.OrginFolio))
             //.ForMember(dest => dest.RelationProcInstId, opt => opt.MapFrom(src => src.OrginProcInstId))
             ;

            //研发物料报废流程
            CreateMap<MaterialDisposalFormData, MaterialDisposalReportDto>()
             .ForMember(dest => dest.SysId, opt => opt.MapFrom(src => Guid.NewGuid()))
             .ForMember(dest => dest.Folio, opt => opt.MapFrom(src => src.FormHeader.ProcFolio!.Trim()))
             .ForMember(dest => dest.Subject, opt => opt.MapFrom(src => src.FormHeader.Subject!.Trim()))
             .ForMember(dest => dest.ProcInstId, opt => opt.MapFrom(src => src.FormHeader.ProcInstId))
             .ForMember(dest => dest.ActivityName, opt => opt.MapFrom(src => src.FormHeader.NextActivityName));


        }
    }
}
